/*   1:    */ package org.intellij.images.util.imageio;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.ArrayUtil;
import org.apache.sanselan.ImageFormat;
import org.apache.sanselan.ImageInfo;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.Sanselan;
import org.apache.sanselan.common.byteSources.ByteSource;

import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */ 
/*  26:    */ public class SanselanImageReaderSpi
/*  27:    */   extends ImageReaderSpi
/*  28:    */ {
/*  29: 39 */   private ThreadLocal<ImageFormat> a = new ThreadLocal();
/*  30:    */   private final List<ImageFormat> b;
/*  31:    */   
/*  32:    */   public SanselanImageReaderSpi()
/*  33:    */   {
/*  34: 44 */     this.vendorName = "JetBrains, s.r.o.";
/*  35: 45 */     this.version = "1.0";
/*  36:    */     
/*  37:    */ 
/*  38:    */ 
/*  39:    */ 
/*  40:    */ 
/*  41: 51 */     this.b = new ArrayList(Arrays.asList(ImageFormat.getAllFormats()));
/*  42: 52 */     this.b.removeAll(Arrays.asList(new ImageFormat[] { ImageFormat.IMAGE_FORMAT_UNKNOWN, ImageFormat.IMAGE_FORMAT_JPEG, ImageFormat.IMAGE_FORMAT_TIFF, ImageFormat.IMAGE_FORMAT_PNG }));
/*  43:    */     
/*  44:    */ 
/*  45:    */ 
/*  46:    */ 
/*  47: 57 */     this.names = new String[this.b.size() * 2];
/*  48: 58 */     this.suffixes = new String[this.b.size()];
/*  49: 59 */     this.MIMETypes = new String[this.b.size()];
/*  50: 60 */     this.pluginClassName = MyImageReader.class.getName();
/*  51: 61 */     this.inputTypes = new Class[] { ImageInputStream.class };
/*  52: 62 */     int i = 0;
/*  53: 62 */     for (int j = this.b.size(); i < j; i++)
/*  54:    */     {
/*  55: 63 */       ImageFormat localImageFormat = (ImageFormat)this.b.get(i);
/*  56: 64 */       this.names[(2 * i)] = localImageFormat.extension.toLowerCase();
/*  57: 65 */       this.names[(2 * i + 1)] = localImageFormat.extension.toUpperCase();
/*  58: 66 */       this.suffixes[i] = this.names[(2 * i)];
/*  59: 67 */       this.MIMETypes[i] = ("image/" + this.names[(2 * i)]);
/*  60:    */     }
/*  61:    */   }
/*  62:    */   
/*  63:    */   public String getDescription(Locale paramLocale)
/*  64:    */   {
/*  65: 72 */     return "Apache Sanselan project based image reader";
/*  66:    */   }
/*  67:    */   
/*  68:    */   public boolean canDecodeInput(Object paramObject)
/*  69:    */     throws IOException
/*  70:    */   {
/*  71: 76 */     if (!(paramObject instanceof ImageInputStream)) {
/*  72: 77 */       return false;
/*  73:    */     }
/*  74: 79 */     ImageInputStream localImageInputStream = (ImageInputStream)paramObject;
/*  75:    */     try
/*  76:    */     {
/*  77: 81 */       ImageFormat localImageFormat = Sanselan.guessFormat(new MyByteSource(localImageInputStream));
/*  78: 82 */       if (this.b.contains(localImageFormat))
/*  79:    */       {
/*  80: 83 */         this.a.set(localImageFormat);
/*  81: 84 */         return true;
/*  82:    */       }
/*  83: 86 */       return false;
/*  84:    */     }
/*  85:    */     catch (ImageReadException localImageReadException)
/*  86:    */     {
/*  87: 89 */       throw new IOException(localImageReadException);
/*  88:    */     }
/*  89:    */   }
/*  90:    */   
/*  91:    */   public ImageReader createReaderInstance(Object paramObject)
/*  92:    */   {
/*  93: 94 */     return new MyImageReader(this, (ImageFormat)this.a.get(), null);
/*  94:    */   }
/*  95:    */   
/*  96:    */   private static class MyByteSource
/*  97:    */     extends ByteSource
/*  98:    */   {
/*  99:    */     private final ImageInputStream a;
/* 100:    */     
/* 101:    */     public MyByteSource(ImageInputStream paramImageInputStream)
/* 102:    */     {
/* 103:101 */       super();
/* 104:102 */       this.a = paramImageInputStream;
/* 105:    */     }
/* 106:    */     
/* 107:    */     public InputStream getInputStream()
/* 108:    */       throws IOException
/* 109:    */     {
/* 110:107 */       this.a.seek(0L);
/* 111:108 */       new InputStream()
/* 112:    */       {
/* 113:    */         public int read()
/* 114:    */           throws IOException
/* 115:    */         {
/* 116:111 */           return SanselanImageReaderSpi.MyByteSource.this.a.read();
/* 117:    */         }
/* 118:    */         
/* 119:    */         public int read(byte[] paramAnonymousArrayOfByte, int paramAnonymousInt1, int paramAnonymousInt2)
/* 120:    */           throws IOException
/* 121:    */         {
/* 122:116 */           return SanselanImageReaderSpi.MyByteSource.this.a.read(paramAnonymousArrayOfByte, paramAnonymousInt1, paramAnonymousInt2);
/* 123:    */         }
/* 124:    */       };
/* 125:    */     }
/* 126:    */     
/* 127:    */     public byte[] getBlock(int paramInt1, int paramInt2)
/* 128:    */       throws IOException
/* 129:    */     {
/* 130:123 */       this.a.seek(paramInt1);
/* 131:124 */       byte[] arrayOfByte = new byte[paramInt2];
/* 132:125 */       int i = this.a.read(arrayOfByte);
/* 133:126 */       return ArrayUtil.realloc(arrayOfByte, i);
/* 134:    */     }
/* 135:    */     
/* 136:    */     public byte[] getAll()
/* 137:    */       throws IOException
/* 138:    */     {
/* 139:131 */       return FileUtil.loadBytes(getInputStream());
/* 140:    */     }
/* 141:    */     
/* 142:    */     public long getLength()
/* 143:    */       throws IOException
/* 144:    */     {
/* 145:136 */       return this.a.length();
/* 146:    */     }
/* 147:    */     
/* 148:    */     public String getDescription()
/* 149:    */     {
/* 150:141 */       return this.a.toString();
/* 151:    */     }
/* 152:    */   }
/* 153:    */   
/* 154:    */   private static class MyImageReader
/* 155:    */     extends ImageReader
/* 156:    */   {
/* 157:    */     private byte[] a;
/* 158:    */     private ImageInfo b;
/* 159:    */     private BufferedImage[] c;
/* 160:    */     private final ImageFormat d;
/* 161:    */     
/* 162:    */     private MyImageReader(SanselanImageReaderSpi paramSanselanImageReaderSpi, ImageFormat paramImageFormat)
/* 163:    */     {
/* 164:152 */       super();
/* 165:153 */       this.d = (paramImageFormat == null ? ImageFormat.IMAGE_FORMAT_UNKNOWN : paramImageFormat);
/* 166:    */     }
/* 167:    */     
/* 168:    */     public void dispose()
/* 169:    */     {
/* 170:158 */       this.a = null;
/* 171:159 */       this.b = null;
/* 172:160 */       this.c = null;
/* 173:    */     }
/* 174:    */     
/* 175:    */     public void setInput(Object paramObject, boolean paramBoolean1, boolean paramBoolean2)
/* 176:    */     {
/* 177:165 */       super.setInput(paramObject, paramBoolean1, paramBoolean2);
/* 178:166 */       this.a = null;
/* 179:167 */       this.b = null;
/* 180:168 */       this.c = null;
/* 181:    */     }
/* 182:    */     
/* 183:    */     private ImageInfo a()
/* 184:    */       throws IOException
/* 185:    */     {
/* 186:172 */       if (this.b == null) {
/* 187:    */         try
/* 188:    */         {
/* 189:174 */           this.b = Sanselan.getImageInfo(b());
/* 190:    */         }
/* 191:    */         catch (ImageReadException localImageReadException)
/* 192:    */         {
/* 193:177 */           throw new IOException(localImageReadException);
/* 194:    */         }
/* 195:    */       }
/* 196:180 */       return this.b;
/* 197:    */     }
/* 198:    */     
/* 199:    */     private byte[] b()
/* 200:    */       throws IOException
/* 201:    */     {
/* 202:184 */       if (this.a == null)
/* 203:    */       {
/* 204:185 */         ImageInputStream localImageInputStream = (ImageInputStream)this.input;
/* 205:186 */         this.a = new SanselanImageReaderSpi.MyByteSource(localImageInputStream).getAll();
/* 206:    */       }
/* 207:188 */       return this.a;
/* 208:    */     }
/* 209:    */     
/* 210:    */     private BufferedImage[] c()
/* 211:    */       throws IOException
/* 212:    */     {
/* 213:192 */       if (this.c == null) {
/* 214:    */         try
/* 215:    */         {
/* 216:194 */           ArrayList localArrayList = Sanselan.getAllBufferedImages(b());
/* 217:195 */           this.c = ((BufferedImage[])localArrayList.toArray(new BufferedImage[localArrayList.size()]));
/* 218:    */         }
/* 219:    */         catch (ImageReadException localImageReadException)
/* 220:    */         {
/* 221:198 */           throw new IOException(localImageReadException);
/* 222:    */         }
/* 223:    */       }
/* 224:201 */       return this.c;
/* 225:    */     }
/* 226:    */     
/* 227:    */     public int getNumImages(boolean paramBoolean)
/* 228:    */       throws IOException
/* 229:    */     {
/* 230:206 */       return a().getNumberOfImages();
/* 231:    */     }
/* 232:    */     
/* 233:    */     public int getWidth(int paramInt)
/* 234:    */       throws IOException
/* 235:    */     {
/* 236:211 */       return a().getWidth();
/* 237:    */     }
/* 238:    */     
/* 239:    */     public int getHeight(int paramInt)
/* 240:    */       throws IOException
/* 241:    */     {
/* 242:216 */       return a().getHeight();
/* 243:    */     }
/* 244:    */     
/* 245:    */     public Iterator<ImageTypeSpecifier> getImageTypes(int paramInt)
/* 246:    */       throws IOException
/* 247:    */     {
/* 248:221 */       return Collections.singletonList(ImageTypeSpecifier.createFromRenderedImage(c()[paramInt])).iterator();
/* 249:    */     }
/* 250:    */     
/* 251:    */     public IIOMetadata getStreamMetadata()
/* 252:    */       throws IOException
/* 253:    */     {
/* 254:226 */       return null;
/* 255:    */     }
/* 256:    */     
/* 257:    */     public IIOMetadata getImageMetadata(int paramInt)
/* 258:    */       throws IOException
/* 259:    */     {
/* 260:231 */       return null;
/* 261:    */     }
/* 262:    */     
/* 263:    */     public BufferedImage read(int paramInt, ImageReadParam paramImageReadParam)
/* 264:    */       throws IOException
/* 265:    */     {
/* 266:236 */       return c()[paramInt];
/* 267:    */     }
/* 268:    */     
/* 269:    */     public String getFormatName()
/* 270:    */       throws IOException
/* 271:    */     {
/* 272:242 */       return this.input == null ? this.d.name : a().getFormat().name;
/* 273:    */     }
/* 274:    */   }
/* 275:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.images.util.imageio.SanselanImageReaderSpi

 * JD-Core Version:    0.7.0.1

 */